/**
 * MagicGrid FixCol Plugin v2.0.1
 * author: Mac_J@boarsoft.com
 * need: grid.js
 */
(function($){
mac.grid_fixCol = function(self) {
	var cfg = self.config;
	// left panel for fixed columns
	var fm = $('<div class="fixed"></div>');
	var fh = $('<div class="head"></div>');
	var fb = $('<div class="body"></div>');
	fm.append(fh);
	fm.append('<div class="clear"></div>');
	fm.append(fb)
	// get elements from grid
	var yb = self.children('.ybar');
	yb.after(fm);
	var mm = self.children('.main');
	var hd = mm.children('.head');
	var bd = mm.children('.body');
	var tt = bd.children('.tt');
	// set col.hidden for reload data
	function toggleColCfg(fns, b) {
		if (!fns || fns.length < 1)
			return;
		$.each(cfg.cols, function(n, c) {
			if ($.inArray((c.name || c.field), fns) > -1) {
				c.hidden = b;
				c.fixed = b;
				return false;// end each
			}
		});
	}
	// clone fixed columns to left panel
	// ift: is first time
	function cloneCols(fns, ift) {
		fh.empty();
		fb.empty();
		var rs = tt.children(), fc = rs.clone(true);
		fc.children('.td').hide();
		$.each(fns, function(n, c) {
			var q = '[name=' + c + ']';
			var h = hd.children('.th' + q);
			var hc = h.clone(true).css('display', 'inline-block');
			hc.find('.sizer').hide();
			h.hide();
			fh.append(hc);
			var x = rs.find('.td' + q).hide();
			fc.children('.td' + q).css('display', 'inline-block');
			var w = ift ? x.width() : 0;
			hd.width(hd.width() - w);
			bd.width(bd.width() - w);
			tt.width(tt.width() - w);
		});
		fb.append(fc);
		fc.children('.td:hidden').remove();
		self.find('.tr').click(function(){
			if(cfg.onRowClick){
				cfg.onRowClick();
				return;
			}
			self.find('.tr').removeClass('selected');
			var tr = self.find('.tr[name=' + $(this).attr('name') + ']');
			self.selected = tr.addClass('selected');
		}).hover(function(){
			self.find('.tr[name=' + $(this).attr('name') + ']').addClass('hover');
		}, function(){
			self.find('.tr[name=' + $(this).attr('name') + ']').removeClass('hover');
		});
	}
	// fns: field names
	self.lockCols = function(fns) {
		fm.show();
		self.fixedCols = fns;
		toggleColCfg(fns, true);
		cloneCols(fns, true);
		// 代理afterLoad事件
		var ual = cfg.afterLoad;
		cfg.afterLoad = function(dd, po, self) {
			cloneCols(fns, false);
			if (ual) {
				ual(dd, po, self);
			}
		}
	}
	self.unlockCols = function() {
		var w = fm.width();
		fh.children().each(function(n, c) {
			hd.seek($(c).remove().attr('name')).show();
		});
		fb.children('.tr').each(function(n, c) {
			var ec = $(c), a = ec.attr('name');
			var x = tt.children('.tr[name=' + a + ']');
			ec.children('.td').each(function(m, t) {
				x.children('.td[name=' + $(t).attr('name') + ']').
					css('display', 'inline-block');
			});
			ec.children('.td:last').removeClass('brx');
		});
		fb.empty();
		hd.width(hd.width() + w);
		bd.width(bd.width() + w);
		tt.width(tt.width() + w);
		toggleColCfg(self.fixedCols, false);
		self.fixedCols = [];
		fm.hide();
	}
	// event
	yb.scroll(function() {
		fb.scrollTop(this.scrollTop)
	});
	self.afterAdjust = function(){
		fb.height(bd.height());
	}
}
})(jQuery);